package cn.com.bluemoon.daps.model.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.common.KeyAnnotationClz;
import cn.com.bluemoon.common.UserPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.model.entity.DapDataModelUserAccredit;
import cn.com.bluemoon.daps.model.service.DapDataModelUserAccreditService;
import cn.com.bluemoon.daps.model.service.impl.DapDataModelUserAccreditServiceImpl;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 模型用户授权表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-02-22
 */
@Api(tags = "模型授权")
@RestController
@Slf4j
@RequestMapping("/modelAccredit")
@BmPermission(value = "", name = "模型用户授权")
public class DapDataModelUserAccreditController {
    @Resource
    private DapDataModelUserAccreditService modelUserAccreditService;

    /**
     * 三张表
     * 1用户表（dap_system_login#userAccount)
     * 2模型表(dap_model#id)
     * 3模型授权表(dap_model_user_accredit#userAccount, dap_model_user_accredit#modelId)
     * output:
     * <pre>
     *     {
     * 	"modelId":"11",
     * 	"data":[
     *        {
     * 	 	"userAccount":"",
     * 	 	"userName":"",
     * 	 	"userEmail":"",
     * 	 	"chosen":true
     *     },
     *     {
     * 	 	"userAccount":"",
     * 	 	"userName":"",
     * 	 	"userEmail":"",
     * 	 	"chosen": false
     *     }
     * 	]
     * }
     * </pre>
     */
    @GetMapping("/list")
    @ApiOperation("模型设计-模型授权")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapDataModelUserAccredit.class)})
    @BmPermission(value = "model:table:authorize", name = "模型设计-获取用户信息")
//    @UserPermission.SingleRow(key = "modelId", keyFormClz = KeyAnnotationClz.RequestParam)
    public ResultBean<List<DapDataModelUserAccreditServiceImpl.ModelUserAccreditVo>> queryList(@ApiParam("模型id") @RequestParam("modelId") String modelId) {
        return ResultBean.ok(modelUserAccreditService.queryUserDtos(modelId));
    }

    @PostMapping("")
    @ApiOperation("模型设计-模型授权-保存（先清除旧后保存新的）")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Boolean.class)})
    @BmPermission(value = "model:table:authorize", name = "模型设计-模型授权")
    @UserPermission.SingleRow(key = "modelId", keyFormClz = KeyAnnotationClz.RequestParam, errorMsg = "模型设计-模型授权")
    public ResultBean<Boolean> save(@ApiParam("模型id") @RequestParam("modelId") String modelId,
                                    @ApiParam("所选用户账号") @RequestBody List<String> accounts) {
        boolean saveBatch = modelUserAccreditService.upsertAccredit(modelId, accounts);
        return ResultBean.ok(saveBatch);
    }


}
